//
//  UIButton+QSExtension.swift
//  TestSwift
//
//  Created by Song on 2018/4/25.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit
import Kingfisher

extension UIButton {
    /** 新增属性key */
    private struct AssociatedKeys {
        static var actionBlockKey: String = "actionBlockKey"
    }
    
    /**
     *  设置按钮背景颜色
     */
    func qs_setBackgroundColor(_ color: UIColor, state: UIControlState) {
        let bgImage = self.qs_createImage(solidColor: color, size: UIScreen.main.bounds.size)
        self.setBackgroundImage(bgImage, for: state)
    }
    
    /// 设置图片
    ///
    /// - Parameters:
    ///   - imgName: 图片
    ///   - placeholder: 占位图
    ///   - state: 状态
    func qs_setImage(with imgName: String, placeholder: String, state: UIControl.State) {
        if imgName.isEmpty {
            self.setImage(UIImage.init(named: placeholder), for: state)
            return
        }
        
        // 网络图片
        if imgName.hasSuffix(".png") || imgName.hasSuffix(".jpg") || imgName.hasSuffix(".jpeg") {
            var urlStr = imgName
            
            if imgName.hasPrefix("http://") || imgName.hasPrefix("https://") {
            } else {
                urlStr = HOST + urlStr
            }
            
            self.kf.setImage(with: ImageResource.init(downloadURL: URL.init(string: urlStr)!), for: state)
        } else {
            self.setImage(UIImage.init(named: placeholder), for: state)
        }
    }
    
    /// 设置背景图片
    ///
    /// - Parameters:
    ///   - imgName: 图片
    ///   - placeholder: 占位图
    ///   - state: 状态
    func qs_setBgImage(with imgName: String, placeholder: String, state: UIControl.State) {
        if imgName.isEmpty {
            self.setBackgroundImage(UIImage.init(named: placeholder), for: state)
            return
        }
        
        // 网络图片
        if imgName.hasSuffix(".png") || imgName.hasSuffix(".jpg") || imgName.hasSuffix(".jpeg") || imgName.hasSuffix(".PNG") || imgName.hasSuffix(".JPG") || imgName.hasSuffix(".JPEG") {
            var urlStr = imgName
            
            if imgName.hasPrefix("http://") || imgName.hasPrefix("https://") {
            } else {
                urlStr = HOST + urlStr
            }
            
            self.kf.setBackgroundImage(with: ImageResource.init(downloadURL: URL.init(string: urlStr)!), for: state, placeholder: UIImage.init(named: placeholder))
        } else {
            self.setBackgroundImage(UIImage.init(named: imgName), for: state)
        }
    }
    
    /**
     *  按钮点击事件
     */
    func qs_setAction(_ action: (@escaping(UIButton) -> ())) {
        objc_setAssociatedObject(self, &AssociatedKeys.actionBlockKey, action, .OBJC_ASSOCIATION_COPY)
        
        self.addTarget(self, action: #selector(self.clickBtn(_:)), for: UIControlEvents.touchUpInside)
    }
    
    // MARK: - Private Methods
    /**
     *  按钮点击事件
     */
    @objc private func clickBtn(_ btn: UIButton) {
        let block = objc_getAssociatedObject(self, &AssociatedKeys.actionBlockKey) as? ((UIButton) -> ())
        
        if block != nil {
            block!(btn)
        }
    }
    
    /**
     *  生成一张纯色的图片
     *
     *  @param solidColor 图片颜色
     *  @param size 图片大小
     *
     *  return 生成的图片
     */
    private func qs_createImage(solidColor: UIColor, size: CGSize) -> UIImage? {
        
        let rect = CGRect.init(origin: CGPoint.init(x: 0, y: 0), size: size)
        UIGraphicsBeginImageContext(size)
        
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(solidColor.cgColor)
        context?.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image
    }
}
